package cn.kgm.makeGodV2.personalityTag;

import cn.kgm.makeGodV2.TagContainer;
import cn.kgm.makeGodV2.emotion.PersonalityTrait;
import lombok.Builder;
import lombok.Data;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 游戏内任何物体,拥有标签功能的物体,都将有这个类
 *
 * @Version 1.8
 * @Author: Kappi
 * @Date: 2025-9-16 11:37
 * @Description: cn.kgm.makeGodV2.personalityTag.PersonalityTagManager
 */
@Data
@Builder
public class PersonalityTagManager {

    // 用户具备的标签
    private final Map<String, PersonalityTag> tags = new HashMap<>();

    /**
     * 单独添加tag标签
     *
     * @param tag 标签
     */
    public void addTag(PersonalityTag tag) {
        tags.put(tag.getId(), tag);
    }

    /**
     * 根据性格特质获取符合的标签
     *
     * @param tagsMap 系统内部所有标签
     * @param traits  npc性格特质
     * @return 符合当前特质的标签
     */
    public List<PersonalityTag> getApplicableTags(Map<String, PersonalityTag> tagsMap, PersonalityTrait traits) {
        if (Objects.isNull(traits)) return new ArrayList<>();
        List<PersonalityTag> applicable = new ArrayList<>();
        for (PersonalityTag tag : tagsMap.values()) {
            if (tag.canApply(traits)) {
                applicable.add(tag);
            }
        }
        // 按优先级排序
        applicable.sort((a, b) -> Integer.compare(b.getPriority(), a.getPriority()));
        return applicable;
    }

    /**
     * 根据npc性格特质初始化标签
     *
     * @param traits 性格特质
     */
    public void initByTraits(PersonalityTrait traits) {
        Map<String, PersonalityTag> tagsMap = TagContainer.getInstance().getTagsMap();
        List<PersonalityTag> applicableTags = getApplicableTags(tagsMap, traits);
        tags.putAll(applicableTags.stream().collect(Collectors.toMap(PersonalityTag::getId, tag -> tag)));
    }
}
